package main

import (
	"errors"
	"fmt"
	"math/rand"
	"strconv"
	"time"
)

type CenterUser interface {
	getCenterId()
}

type WechatUser struct {
	Id         int64
	OpenId     string
	NickName   string
	Avatar     string
	Mobile     string
	Gender     int
	IsSub      int
	CreateTime time.Time
}

type User struct {
	Id         int64
	NickName   string
	Avatar     string
	Mobile     string
	Gender     int
	CreateTime time.Time
}

func (wechatUser WechatUser) getCenterId() {
	fmt.Println("center id is 1")
}

func (user User) getCenterId() {
	fmt.Println("center id is 2")
}

func sqrt(f float64) (float64, error) {
	if f < 0 {
		return 0, errors.New("what ???")
	}
	return f / 2, nil
}

type CustomError struct {
	eCode int8
	eMsg  string
}

func (ce *CustomError) Error() string {
	strFormat := `{"code": %d,"msg": %s}`
	return fmt.Sprintf(strFormat, ce.eCode, ce.eMsg)
}

// 函　数：生成随机数
// 概　要：
// 参　数：
//      max: 最大值
// 返回值：
//      int: 生成的随机数 [0,max)
func RandInt(max int) int {
	rand.Seed(time.Now().Unix())
	return rand.Intn(max)
}

func main() {
	//fmt.Println(len(os.Args))
	//// 输出 1
	//for _, arg := range os.Args {
	//	fmt.Println(arg)
	//	// 输出 C:\Users\123\AppData\Local\Temp\go-build161314094\b001\exe\test.exe
	//}

	//addWxUser()
	testInterface()
	testError()
	result := testCustomError(RandInt(2))
	fmt.Println(result)

}

func testCustomError(showError int) (result string) {
	if showError > 0 {
		eObj := CustomError{
			eCode: -1,
			eMsg:  "有问题啊" + strconv.Itoa(showError),
		}
		return eObj.Error()
	}
	return "not error " + strconv.Itoa(showError)
}

func testError() {
	result, err := sqrt(100)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(result)
	}
}

func testInterface() {
	var centerUser CenterUser

	centerUser = new(WechatUser)
	centerUser.getCenterId()

	centerUser = new(User)
	centerUser.getCenterId()
}

func addWxUser() {
	var testUser = WechatUser{
		Id:         1,
		OpenId:     "oxXrNwzJAzcsVch8n_SEilODS-rk",
		Mobile:     "13566667777",
		NickName:   "李义全",
		CreateTime: time.Now(),
	}
	var userList map[string]WechatUser
	userList = make(map[string]WechatUser)
	userList["lyq"] = testUser
	for userKey := range userList {
		fmt.Println(userKey, "用户名称：", userList[userKey].NickName)
		fmt.Println(userKey, "手机号码：", userList[userKey].Mobile)
	}

	value, isExist := userList["lyq"]
	fmt.Println(isExist)
	if isExist {
		fmt.Println("value", value)
	}
}
